/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TinTuc;

import java.io.*;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.*;

/**
 *
 * @author Admin
 */
public class XPathWriter {

    protected String domain;
    protected String rootroot;
    protected String broot;
    protected String aroot;
    protected String img;
    protected String date;
    protected String summary;
    protected XMLOutputter printer = null;
    protected File xmlFile;
    protected static Document doc;

    public XPathWriter() {
        this.printer = new XMLOutputter();
    }

    public XPathWriter(File xmlFile) throws JDOMException, IOException {
        SAXBuilder parser = new SAXBuilder();
        this.printer = new XMLOutputter();
        this.xmlFile = xmlFile;
        XPathWriter.doc = parser.build(xmlFile);
    }
    //<editor-fold desc="Insert">

    public void Write() throws JDOMException, IOException {
        SAXBuilder parser = new SAXBuilder();
        try {
            File fi=new File("cautrucxpathcuatrangweb.xml");
            Element domain = new Element("xpath");
            Document doc = new Document(domain);
            XMLOutputter xmOut = new XMLOutputter();
            xmOut.setFormat(Format.getPrettyFormat());
            BufferedWriter bw = new BufferedWriter(new FileWriter(fi));
            String xmlOut = xmOut.outputString(doc);
            bw.write(xmlOut);
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean InsertDomain(String domainString) {
        XPathReader xr = new XPathReader(xmlFile);
        if (xr.getDomainElement(XPathWriter.doc, domainString) != null) {
            return false;
        }
        try {
            //Document doc = parser.build(xmlFile);
            Element Root = XPathWriter.doc.getRootElement();
            Element e = new Element("domain");
            e.setAttribute("id", domainString);
            Root.addContent(e);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
        }
        return true;
    }

    public boolean InsertRootRoot(String domainName, String rrString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getRootRootElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("rootroot");
            rrE.setText(rrString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertARoot(String domainName, String ARootString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getARootElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("aroot");
            rrE.setText(ARootString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertBRoot(String domainName, String BRootString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getBRootElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("broot");
            rrE.setText(BRootString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertTitle(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getTitleElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("title");
            rrE.setText(TitleString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertLink(String domainName, String LinkString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getLinkElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("link");
            rrE.setText(LinkString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertImg(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getImgElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("img");
            rrE.setText(TitleString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertDate(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getDateElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("date");
            rrE.setText(TitleString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean InsertSummary(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getSummaryElement(eDomain) != null) {
                return false;
            }
            Element rrE = new Element("summary");
            rrE.setText(TitleString);
            eDomain.addContent(rrE);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    //</editor-fold>
    //<editor-fold desc="Update">

    public boolean UpdateDomain(String domainString, String newid) {
        XPathReader xr = new XPathReader();
        if (xr.getDomainElement(XPathWriter.doc, domainString) == null) {
            return false;
        }
        try {
            Element eDomain = xr.getDomainElement(XPathWriter.doc, domainString);
            eDomain.setAttribute("id", newid);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateRootRoot(String domainName, String rrString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getRootRootElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(rrString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateARoot(String domainName, String ARootString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            if (rd.getARootElement(eDomain) == null) {
                return false;
            }
            Element rrE = rd.getARootElement(eDomain);
            rrE.setText(ARootString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateBRoot(String domainName, String BRootString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getBRootElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(BRootString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateTitle(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getTitleElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(TitleString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateLink(String domainName, String LinkString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getLinkElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(LinkString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateImg(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getImgElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(TitleString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateDate(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getDateElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(TitleString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean UpdateSummary(String domainName, String TitleString) {
        XPathReader rd = new XPathReader();
        try {
            Element eDomain = rd.getDomainElement(XPathWriter.doc, domainName);
            Element e = rd.getSummaryElement(eDomain);
            if (e == null) {
                return false;
            }
            Element rrE = e;
            rrE.setText(TitleString);
            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(XPathWriter.doc, new FileWriter(
                    this.xmlFile.getName()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    //</editor-fold>
}
